<template>
    <!-- 分公司选择 -->
    <a-select
        v-bind="$attrs"
        v-on="$listeners"
        :placeholder="placeholder"
        v-model="currentValue"
    >
        <a-icon v-if="fetching" style="color: #096dd9" slot="suffixIcon" type="loading" />
        <a-spin v-if="fetching" slot="notFoundContent" size="small" />
        <a-select-option selected v-for="i in department" :key="i.id" :value="i.id">{{ i.label }}</a-select-option>
    </a-select>
</template>
<script>
import { getCustOrgTree } from '@/api/subassembly/TreeSelectOrganization'
/*
* SelectDepartment2.vue,选择公司层级所有部门
*
* */
export default {
    name: 'SelectDepartment2',
    props: {
        // v-model绑定的数据
        value: {
            type: [Array, String, Object]
        },
        // placeholder
        placeholder: {
            type: String,
            default: function() {
                return '请选择'
            }
        },
        // 是否自动选中第一行
        autoSelected: {
            type: Boolean,
            default: () => true
        }
    },
    data() {
        return {
            fetching: false,
            department: []
        }
    },
    mounted() {
        this.getCompanyList()
    },
    methods: {
        async getCompanyList() {
            let res = await getCustOrgTree({ containVirtual: 0 })
            let company = res.data.find(v=>v.data.code === 'CSCECXN')

            let department = company.children.filter(v => v.type === '2')
            department.map(v => delete v.data)
            this.department = department
            if (this.autoSelected && !this.currentValue) {
                this.currentValue = department[0].id
            }
        },

        filterOption(input, option) {
            return (
                option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
            )
        }
    },
    computed: {
        // 转换绑定的选中数据，与v-model同步
        currentValue: {
            get: function() {
                return this.value
            },
            set: function(value) {
                this.$emit('input', value)
            }
        }
    }
}
</script>
